/**
 * @author : HESS MArjorie, THUILLIER Jules, G4 - CIP2
 */
package UserInterface;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import linkTreeInterface.Word;

import ArbreBinaire.ArbreVideException;


public class SecondFrame extends JFrame implements ActionListener{

   private String text;
   private String trad;
   private JTextField result_TextField;
    
    public SecondFrame(String title, String text, int heigth) throws ArbreVideException{
    	this.text = text;
    	
    	this.setTitle(title);
	    this.setSize(350, heigth);

	    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    this.setLocationRelativeTo(null);
	    
	    JButton edit_Button;
	    
	    
	    if(title=="Edit"){ 
	    	this.trad = UserInterface.Main.arbreWord.findTrad(UserInterface.Main.arbreBinaire,text);
	    	 result_TextField = new JTextField(trad, 20);
	    	 edit_Button = new JButton("Edit");
	    }
	    else if(title=="Translation"){
	    	 result_TextField = new JTextField("", 20);
	    	 edit_Button = new JButton("Ok");
	    }
	    else edit_Button = new JButton("ERROR");
	    
	    JButton cancel_Button = new JButton("Cancel");
	    
	    final JFrame edit_Frame = new JFrame("Dictionary");  
		    

	      JPanel mainPanel = (JPanel)edit_Frame.getContentPane();  

	      
	      	mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
	      	mainPanel.setBorder( new EmptyBorder(new Insets(20, 20, 10, 20)));
	      
	      	mainPanel.add(Box.createVerticalGlue());
	      	add(mainPanel);
  
	      if(title=="Translation"){
	    	mainPanel.add(new JLabel(text + " :"));
	      	mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));}
	      
		    mainPanel.add(result_TextField);
		    
		    JPanel bottom_Panel = new JPanel();
	 
		    bottom_Panel.setLayout(new BoxLayout(bottom_Panel, BoxLayout.X_AXIS)); 
		    bottom_Panel.add(edit_Button);
	      	bottom_Panel.add(cancel_Button);
	      	
	      	mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
	      	mainPanel.add(bottom_Panel);
	      	
	      	
	      	edit_Frame.pack();
	      	this.setVisible(true);
	      	edit_Button.addActionListener(this);
		    cancel_Button.addActionListener(this);
	    
    }

public void actionPerformed(ActionEvent e) {
	
	if(e.getActionCommand() == "Cancel"){
		// Si on clique sur le bouton "Cancel"
		
		this.dispose();
	}
	
	else if(e.getActionCommand() == "Edit"){
		// Si on clique sur le bouton "Edit"
		try {
			UserInterface.Main.arbreWord.editer(UserInterface.Main.arbreBinaire,text, result_TextField.getText());
		} catch (ArbreVideException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
		this.dispose();
	}
	
	else if(e.getActionCommand() == "Ok"){
		// Si on clique sur le bouton "Ok"
		try {
			if(result_TextField.getText().length()!=0){
				UserInterface.Main.arbreBinaire = UserInterface.Main.arbreWord.ajouterEnFeuilleR(UserInterface.Main.arbreBinaire, new Word(text, result_TextField.getText()));
			UserInterface.MainFrame.search_Box.setText("");
			this.dispose();
			}
			else new UserInterface.ErrorDialog("Please write the translation");
		} catch (ArbreVideException e1) {
			e1.printStackTrace();
		}
	}
}	
	
}
